from selenium import webdriver
from ddddocr import DdddOcr
import time
import pyautogui
import requests
import time
import urllib.parse
import hmac
import json
import hashlib
import base64

options = webdriver.FirefoxOptions()
# options.add_experimental_option('detach', True)
options.add_argument('--log-level=3')
# options.add_argument('--headless')

'''
参数配置处
'''
#   地区配置处：
adress = '杭州电子'
#   起爆器编号修改处：
num = 'F9900000289'
#   雷管编号配置处：
lg_num = '99201226000002'
#   数量配置处：
duoshao = '1'
#   通知人姓名首字母
name = 'wh'

drive = webdriver.Firefox(options=options)
drive.maximize_window()
drive.get('http://test.qamltech.com/login.html')

drive.implicitly_wait(10)
drive.find_element('id', 'dlm').send_keys('13988888881')
drive.find_element('id', 'mm').send_keys('123456')
tp = drive.find_element('xpath', '//*[@id="code"]')
tp.screenshot('qayzm.jpg')
ocr = DdddOcr()
with open('../qayzm.jpg', 'rb') as f:
    img = f.read()
res = ocr.classification(img)
drive.find_element('id', 'yzm').send_keys(res)
drive.find_element('id', 'tjdl').click()
while True:
    try:
        time.sleep(2)
        alter2 = drive.switch_to.alert
        alter2.accept()

        if drive.find_element('id', 'tjdl') != []:
            tp.screenshot('qayzm.jpg')
            ocr = DdddOcr()
            with open('../qayzm.jpg', 'rb') as f:
                img1 = f.read()
            res1 = ocr.classification(img1)
            drive.find_element('id', 'yzm').send_keys(res1)
            drive.find_element('id', 'tjdl').click()
    except Exception as e:
        pass
        break
time.sleep(6)
[pyautogui.hotkey('ctrl', '-') for o in range(3)]
time.sleep(1)
drive.find_element('xpath', '/html/body/div/div[3]/div[1]/div/div/nav/ul/li[5]/a/span').click()
time.sleep(1)
drive.find_element('xpath',
                   '/html/body/div/div[3]/div[2]/div[2]/div/div/div[1]/div[1]/span[2]/button').click()
time.sleep(1)
drive.find_element('xpath', '/html/body/div[3]/div/div/div[2]/form/div[1]/div[2]/div[1]/div/input').send_keys(
    adress)  # 可以修改

time.sleep(5)
drive.find_element('xpath',
                   '/html/body/div[3]/div/div/div[2]/form/div[1]/div[2]/div[1]/div/ul/li/a').click()
time.sleep(2)
drive.switch_to.alert.accept()
time.sleep(1)
drive.switch_to.alert.accept()
time.sleep(1)
drive.find_element('xpath',
                   '/html/body/div[3]/div/div/div[2]/form/div[1]/div[2]/div[2]/div/div/span/button').click()
time.sleep(3)
drive.find_element('xpath',
                   '/html/body/div[3]/div/div/div[2]/form/div[1]/div[2]/div[2]/div/div/ul/li[2]/span/button[1]').click()

drive.find_element('xpath', '/html/body/div[3]/div/div/div[2]/form/div[2]/div/div/div/input').send_keys(num)

while True:
    if drive.find_elements('xpath', '/html/body/div[3]/div/div/div[2]/form/div[2]/div/div/ul/li/div[4]/a/div') != []:
        break
for i in range(4, 200000):
    # print(i)
    if drive.find_element('xpath',
                          f'/html/body/div[3]/div/div/div[2]/form/div[2]/div/div/ul/li/div[{i}]/a/div').text == num:
        drive.find_element('xpath',
                           f'/html/body/div[3]/div/div/div[2]/form/div[2]/div/div/ul/li/div[{i}]/a/div').click()
        break
time.sleep(1)
drive.find_element('xpath',
                   '/html/body/div[3]/div/div/div[2]/form/div[3]/div/div/div/div/table/tbody/tr/td[1]/input').send_keys(
    '2')
time.sleep(1)
# drive.find_element('xpath','/html/body/div[3]/div/div/div[2]/form/div[3]/div/div/div/div/table/tbody/tr/td[2]/input').send_keys('16')
time.sleep(1)
drive.find_element('xpath',
                   '/html/body/div[3]/div/div/div[2]/form/div[3]/div/div/div/div/table/tbody/tr/td[4]/a').click()
time.sleep(1)
drive.find_element('xpath', '/html/body/div[3]/div/div/div[1]/table/tbody/tr[2]/td/input[1]').send_keys(
    lg_num)  # 输入雷管编号
time.sleep(1)
drive.find_element('xpath', '/html/body/div[3]/div/div/div[1]/table/tbody/tr[2]/td/input[2]').send_keys(duoshao)  # 输入数量
time.sleep(1)
drive.find_element('xpath', '/html/body/div[3]/div/div/div[1]/table/tbody/tr[2]/td/button').click()
time.sleep(1)
drive.find_element('xpath', '/html/body/div[3]/div/div/div[1]/table/tbody/tr[4]/td[3]/button').click()
time.sleep(1)
drive.find_element('xpath', '/html/body/div[3]/div/div/div[2]/form/div[4]/button').click()
time.sleep(2)
drive.find_element('xpath',
                   '/html/body/div/div[3]/div[2]/div[2]/div/div/div[1]/div[2]/div/div[2]/div/div[1]/div[9]/div[2]/span/a[3]').click()
time.sleep(1)
drive.switch_to.alert.accept()
time.sleep(1)
drive.switch_to.alert.accept()
time.sleep(1)
drive.find_element('xpath', '/html/body/div[3]/div/div/div[2]/form/div[4]/button[1]').click()
time.sleep(1)
drive.find_element('xpath', '/html/body/div/div[3]/div[1]/div/div/nav/ul/li[3]/a').click()
time.sleep(1)
# drive.find_element('xpath', '/html/body/div/div[3]/div[1]/div/div/nav/ul/li[3]/ul/li/a/span').click()
drive.find_element('xpath', '/html/body/div/div[3]/div[1]/div/div/nav/ul/li[3]/ul/li/a/span').click()
time.sleep(1)
a = drive.find_element('xpath',
                       '/html/body/div/div[3]/div[2]/div[2]/div/div[2]/div[2]/div/div[2]/div/div[1]/div[7]/div[2]/div/span').text
drive.close()

appkey = 'dingd1tuwe1vub82dewa'
appsecret = 'AiFb5L_T33UshMsxN24h1Qdb7wyhLpTaNuDPa_bFW61o2IPQxdOOS4eUQ5R7aYv3'
url = 'https://oapi.dingtalk.com/gettoken?appkey=' + appkey + '&appsecret=' + appsecret
response = requests.get(url=url)
result = response.json()
errmsg = result['errmsg']
# print('获取密钥是否成功：', errmsg)
try:
    access_token = result['access_token']
except Exception as e:
    access_token = ''

user_id = {'zxl': '304053521224173706', 'hxy': '525820011532470245', 'lf': '011237155425672457',
           'xcm': '525242255024317504', 'xzh': '024127040131973956', 'wh': '014101546962953694',
           'cjh': '234859304538011107'}
url = 'https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=' + access_token
_data = {
    'agent_id': '2813022307',
    'userid_list': user_id[name],
    'msg': {
        'msgtype': 'markdown',
        'markdown': {
            "title": "自动化测试结果通知！",
            "text": '雷管授权验证码：' + a + ' \n 结束时间： ' + time.strftime('%Y-%m-%d %H:%M:%S')
        }}
}
data = json.dumps(_data)
response = requests.post(url=url, data=data)
result = response.json()
# print(result)
